package Sort;

import java.util.Stack;

public class Sort {
    //插入排序
    public static void insert(int[] array){
        for (int i = 1; i < array.length; i++) {
            int temp=array[i];
            int j=i-1;
            for (; j >=0 ; j--) {
                if(array[i]<temp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=temp;
        }
    }
    //希尔排序   跳跃式分组,小的往前走,大的往后走
    public static void shellSort(int[] array){
        int gap=array.length;
        while(gap>1){
            gap=gap/3+1;
            shellInsertSort(array,gap);

        }
    }
    private static void shellInsertSort(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int temp=array[gap];
            int j=i-gap;
            for (; j >=0 ; j--) {
                if(array[i]<temp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
            }
            array[j+gap]=temp;
        }
    }
    public static void swap(int[] array,int x,int y){
        int temp=array[x];
        array[x]=array[y];
        array[y]=temp;
    }

    //选择排序  方法1
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int j=i+1;
            int minIndex=i;
            for (;j<array.length;j++){
                if(array[j]>array[minIndex]){
                    minIndex=j;
                }
            }
            swap(array,minIndex,i);
        }
    }
    //方法二
    //思路:就是同时找最大值和最小值,最大值往后放,最小值往前放
    public static void selectSort2(int[] array){
        int left=0;
        int right=array.length-1;
        while (left<right){
            int minIndex=left;
            int maxIndex=left;
            for (int i = left+1; i <array.length ; i++) {
                if(array[i]<array[minIndex]){
                    minIndex=i;
                }
                if(array[i]>array[maxIndex]){
                    maxIndex=i;
                }
            }
            swap(array,minIndex,left);
            if(maxIndex==left){
                maxIndex=minIndex;
            }
            swap(array,maxIndex,right);
        }
        left++;
        right--;
    }
    //堆排序
    //创建堆
    public static void createHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shifDown(array,parent,array.length-1);
        }
    }
    public static void shifDown(int[] array,int parent,int end){
        int child=parent*2+1;
        while(child<end){
            if(child+1<end&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,child,parent);
                parent=child;
                child=parent*2+1;
            }else{
                break;
            }
        }
    }
    public static void heapSort(int[] array){
        createHeap(array);
        int end=array.length-1;
        while(end>=0){
            swap(array,0,end);
            shifDown(array,0,end);
            end--;
        }
    }
    //冒泡排序
    public static void BubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flag=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]<array[j+1]){
                    swap(array,j+1,j);
                }else {
                    flag=true;
                }
            }
            if(flag==true){
                break;
            }
        }
    }
    //快排
//    public static void quickSort(int[] array){
//
//    }
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }

        if(end - start + 1 <= 10) {
            insertSortRange(array,start,end);
            return;
        }

        //System.out.println("start: "+start);
        //System.out.println("end: "+end);
        //1 2 3 4 5 6
        //三数取中
        int index = midThreeNum(array,start,end);
        swap(array,index,start);

        //3 2 1 4 5
        int par = partition(array,start,end);

        quick(array,start,par-1);
        quick(array,par+1,end);
    }

    public static void insertSortRange(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= left ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    private static int midThreeNum(int[] array,int left,int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]) {
            if(array[mid] <array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    //挖坑法
    private static int partition(int[] array,int left,int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];

            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    private static int partitionHoare(int[] array,int left,int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            //array[right] >= tmp  这里能不能 不取等于号
            // right--; 为什么先走右边
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right &&  array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        //left 和 right 相遇了
        swap(array,left,i);
        return left;
    }
    //前后指针法
    private static int partitionPre(int[] array, int left, int right) {
        int prev = left ;
        int cur = left+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int par = partition(array,left,right);
        if(par > left + 1) {
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right - 1) {
            stack.push(par+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if(par > left + 1) {
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right - 1) {
                stack.push(par+1);
                stack.push(right);
            }
        }
    }
    public static void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }

    public static void mergeSortFun(int[] array,int left,int right) {
        if(left >= right) {// >
            return;
        }
        int mid = (right+left) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        //合并
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,
                              int mid,int right) {
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //走到这里 相当于tmp数组 当中 所有的元素 都有序了
        //接下来把tmp数组的内容 拷贝到array数组当中
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }
    /**
     * 非递归实现归并排序
     * @param array
     */
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left+gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid+gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    /**
     * 计数排序
     * 时间复杂度：O(N+范围)
     * 空间复杂度：O(范围)
     * 稳定性：稳定
     * @param array
     */
    public static void countSort(int[] array) {
        //1. 遍历数组 求最大值 和 最小值
        int maxVal = array[0];
        int minVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if(maxVal < array[i]) {
                maxVal = array[i];
            }
            if(minVal > array[i]) {
                minVal = array[i];
            }
        }
        //2. 定义count数组
        int[] count = new int[maxVal - minVal + 1];
        //3. 遍历array数组 把值 放入 计数数组当中
        for (int i = 0; i < array.length; i++) {
            int val = array[i];//98
            count[val - minVal]++;
        }
        //4. 以上3步完成之后，计数数组 已经存好了对应的数据
        // 接下来 开始遍历 计数数组
        int index = 0;//array的下标
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }

}
